package com.hoshiicloud.vericenter.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hoshiicloud.vericenter.entity.SysMessages;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Param;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 短信功能 Mapper 接口
 * </p>
 *
 * @author Mr.Wu
 * @since 2019-05-20
 */
public interface SysMessagesMapper extends BaseMapper<SysMessages> {
    @InsertProvider(type = SysMessagesSqlBuilder.class, method = "batchInsert")
    public int batchInsert(@Param("list") List<SysMessages> list);

    class SysMessagesSqlBuilder {
        public String batchInsert(Map map) {
            List<SysMessages> list=(List<SysMessages>) map.get("list");
            StringBuilder sb = new StringBuilder();
            sb.append("INSERT INTO sys_messages ");
            sb.append("(id, mobile, context,rand_code,type,sent_time,expires_time,available) ");
            sb.append("VALUES ");
            MessageFormat mf = new MessageFormat("(#'{'list[{0}].id},#'{'list[{0}].mobile},#'{'list[{0}].context},#'{'list[{0}].randCode}," +
                    "#'{'list[{0}].type},#'{'list[{0}].sentTime},#'{'list[{0}].expiresTime},#'{'list[{0}].available})");
            for (int i = 0; i < list.size(); i++) {
                sb.append(mf.format(new Object[]{i}));
                if (i < list.size() - 1) {
                    sb.append(",");
                }
            }
            return sb.toString();
        }
    }
}
